package com.hdcircles.autoform.presenter;

import com.hdcircles.autoform.base.RxPresenter;
import com.hdcircles.autoform.model.bean.ChooseQuestionBean;
import com.hdcircles.autoform.model.bean.QuestionBean;
import com.hdcircles.autoform.presenter.contract.MultipleChoiceContract;
import com.hdcircles.autoform.util.LogUtil;
import com.hdcircles.autoform.util.RxUtil;
import com.hdcircles.autoform.widget.ChoiceAnswerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by hdcircles on 2016/9/29.
 */

public class MultipleChoicePresenter extends RxPresenter<MultipleChoiceContract.View> implements MultipleChoiceContract.Presenter {


    @Inject
    public MultipleChoicePresenter(){}

    @Override
    public boolean validate(List<ChoiceAnswerView> answerViewList) {
        final List<ChoiceAnswerView> answerList=new ArrayList<>();
        answerList.addAll(answerViewList);
        Subscription subscription= Observable.from(answerList)
                .compose(RxUtil.<ChoiceAnswerView>rxScheduleHelper())
                .filter(new Func1<ChoiceAnswerView, Boolean>() {
                    @Override
                    public Boolean call(ChoiceAnswerView choiceAnswerView) {
                        return choiceAnswerView.getEtAnswer().getText().length()==0;
                    }
                })
                .subscribe(new Action1<ChoiceAnswerView>() {
                    @Override
                    public void call(ChoiceAnswerView choiceAnswerView) {
                        choiceAnswerView.getEtAnswer().setError("不能为空");
                        answerList.remove(choiceAnswerView);
                    }
                });
        addSubscribe(subscription);
        return answerList.size() == answerViewList.size();
    }

    @Override
    public void genQuestion(List<ChoiceAnswerView> answerViewList, final QuestionBean questionBean) {
        final List<ChooseQuestionBean> choiceList=new ArrayList<>();
        Subscription subscription=Observable.from(answerViewList)
                .compose(RxUtil.<ChoiceAnswerView>rxScheduleHelper())
                .map(new Func1<ChoiceAnswerView, ChooseQuestionBean>() {
                    @Override
                    public ChooseQuestionBean call(ChoiceAnswerView choiceAnswerView) {
                        ChooseQuestionBean choice=new ChooseQuestionBean();
                        String answer=choiceAnswerView.getEtAnswer().getText().toString().trim();
                        choice.setInfo(answer);
                        return choice;
                    }
                })
                .subscribe(new Observer<ChooseQuestionBean>() {
                    @Override
                    public void onCompleted() {
                        LogUtil.d("onCompleted");
                        questionBean.setChooseQuestion(choiceList);
                        mView.toSurvey(questionBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e(e.getMessage());
                    }

                    @Override
                    public void onNext(ChooseQuestionBean chooseQuestionBean) {
                        choiceList.add(chooseQuestionBean);
                        LogUtil.d("onNext/"+choiceList.size());
                    }
                });
        addSubscribe(subscription);
    }
}
